$(document).ready(function() {
	
	var photo = document.forms['MODIFYPROFIL'].PHOTO;
	var pseudo = document.forms['MODIFYPROFIL'].PSEUDO;
	var password1 = document.forms['MODIFYPROFIL'].PASSWORD1;
	var password2 = document.forms['MODIFYPROFIL'].PASSWORD2;
	var jour = document.forms['MODIFYPROFIL'].JOUR;
	var mois = document.forms['MODIFYPROFIL'].MOIS;
	var annee = document.forms['MODIFYPROFIL'].ANNEE;
	var promo = document.forms['MODIFYPROFIL'].PROMOTION;
	var formation = document.forms['MODIFYPROFIL'].FORMATION;
	
	var photoOK=false, pseudoOK=false, birthdateOK=false, pass1OK=false, pass2OK=false, promoOK=false, formationOK=false;
	var photoChanged=false, pseudoChanged=false, birthdateChanged=false, passwordChanged=false, promoChanged=false;
	
	$(".edit").click(function() {
		var edit_obj = $(this);
		var obj = $(this).parent().children("td:first");
		var isChanged = false;
		
		if(obj.has(".invisible").length>0) {
			obj.children("span").text("");
			obj.children("input[type=text], input[type=password], select").removeClass("invisible");
			isChanged=true;
		} else {
			obj.children("span").text(obj.children("input[type=hidden]").attr("value"));
			obj.children("input[type=text], input[type=password], select").addClass("invisible");
			isChanged=false;
		}
		
		switch(obj.children("input[type=text], input[type=password]:first").attr("name")) {
		case "PSEUDO":
			pseudoChanged = isChanged;
			break;
		case "PASSWORD1":
			passwordChanged = isChanged;
			break;
		}
		switch(obj.children("select:first").attr("name")) {
			case "JOUR":
				birthdateChanged = isChanged;
				break;
			case "PROMOTION":
				promoChanged = isChanged;
				break;
		}
	});
	
	$("input[type=file]").click(function() {
		if(photoChanged)
			photoChanged=false;
		else
			photoChanged=true;
	});
	
	$("input[name=PSEUDO]").blur(function() {
		var obj = $(this);
		if(isMinimumCharactersEntered(pseudo.value)) {
			$.ajax( {
				type: "GET",
				url: "index.php?control=user&action=checkpseudo",
				data: "pseudo="+$(this).attr('value'),
				beforeSend: function() {
					obj.parent().parent().children('.error').html('<img src="images/preloader.gif" alt="" title=""/>');
				},
				success: function(msg){
					if(msg=="ok" || obj.attr("value")==obj.parent().children("input[type=hidden]").attr("value")) {
						if(pseudoChanged)
							obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
						obj.css('box-shadow','0px 0px 1px 2px #2cd659');
						pseudoOK=true;
					}
					else {
						if(pseudoChanged)
							obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Déja pris');
						obj.css('box-shadow','0px 0px 1px 2px #fb9000');
						pseudoOK=false;
					}
				}
			});
		} else {
			if(pseudoChanged)
				obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>2 caractères minimum');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pseudoOK=false;
		}
	});
	
	$('input[name=PASSWORD1]').blur(function() {
		var obj = $(this);
		if(isGoodPassword(obj.attr('value'))) {
			if(passwordChanged)
				obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
			obj.css('box-shadow','0px 0px 1px 2px #2cd659');
			pass1OK=true;
		} 
		else {
			if(passwordChanged)
				obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>6 caractères minimum');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pass1OK=false;
		}
	});
	
	// VALIDATION DE LA REFRAPPE DU MOT DE PASSE
	$('input[name=PASSWORD2]').blur(function() {
		var obj = $(this);
		if(isPasswordOK(password1.value, password2.value)) {
			if(passwordChanged)
				obj.parent().parent().children('.error').html('<img src="images/checked.png" alt=""/>');
			obj.css('box-shadow','0px 0px 1px 2px #2cd659');
			pass2OK=true;
		} 
		else {
			if(passwordChanged)
				obj.parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Mots de passe différents');
			obj.css('box-shadow','0px 0px 1px 2px #fb9000');
			pass2OK=false;
		}
	});
	
	function isValideDate() {
		if(annee.value!='default' && mois.value!='default' && jour.value!='default') {
			return true;
		}
		else {
			if(birthdateChanged)
				$('select[name=ANNEE]').parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Date non valide');
			return false;
		}
	}
	
	function isGoodPromo() {
		if(promo.value!='default') 
			return true;
		else {
			if(promoChanged)
				$('select[name=PROMOTION]').parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Entrez une promo');
			return false;
		}
	}
	
	function isGoodFormation() {
		if(formation.value!='default')
			return true;
		else {
			if(promoChanged)
				$('select[name=FORMATION]').parent().parent().children('.error').html('<img src="images/fail.png" alt=""/>Entrez une formation');
			return false;
		}
	}
	
	function modifyprofil(modifs) {
		$.ajax( {
			type: "GET",
			url: "index.php?control=user&action=modifyProfil",
			data: modifs+"",
			success: function(msg){
				$("#infos").html("Les modifications ont été apportées. Elles seront actives à votre prochaine connexion.");
			}
		});
	}
	
	function uploadphoto() {
		$.ajax( {
			type: "POST",
			url: "index.php?control=user&action=uploadPhoto",
			success: function(msg){
				alert("Upload réussi");
				//alert(promoChanged+" "+promoOK+" "+formationOK);
				//$("#infos").html("index.php?control=user&action=modifyProfil"+modifs+"  Les modifications ont été apportées. Elles seront actives à votre prochaine connexion.");
			}
		});
	}
	
	$('form[name=MODIFYPROFIL]').submit(function() {
		birthdateOK = isValideDate();
		promoOK = isGoodPromo();
		formationOK = isGoodFormation();
		
		var modifs = "";
		if(pseudoChanged && pseudoOK)
			modifs = modifs+"&pseudo="+pseudo.value;
		if(passwordChanged && pass1OK && pass2OK)
			modifs = modifs+"&pass="+password1.value;
		if(birthdateChanged && birthdateOK)
			modifs = modifs+"&birthdate="+annee.value+"-"+mois.value+"-"+jour.value;
		if(promoChanged && promoOK && formationOK)
			modifs = modifs+"&promo="+promo.value+"&formation="+formation.value;
		if(photoChanged)
			uploadphoto();
		
		modifyprofil(modifs);
		
		return false;
	});
	
	
	
	
});